package com.seveneight.baijiesteward.ui.main.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.PlanListAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.RankListBean;
import com.seveneight.baijiesteward.bean.RepayPlanBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.model.PlanListModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PlanPayListActivity extends BaseActivity implements AdapterView.OnItemClickListener{

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.add_more)
    ImageView add_more;
    @BindView(R.id.plan_success_view)
    View plan_success_view;
    @BindView(R.id.plan_waiting_view)
    View plan_waiting_view;
    @BindView(R.id.plan_performed_view)
    View plan_performed_view;
    @BindView(R.id.paln_error_view)
    View plan_error_view;
    @BindView(R.id.plan_all_view)
    View plan_all_view;
    @BindView(R.id.plan_list)
    ListView listView;
//    SpringView springView;
    List<RankListBean> listData=new ArrayList<>();
    PlanListAdapter mAdapter;
    PlanListModel api=new PlanListModel();
    int pageIndex=1;
    int status=0;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_plan_pay_list);
    }

    @Override
    protected void initView() {
        tv_title.setText("计划列表");
        add_more.setVisibility(View.VISIBLE);
//        springView=findViewById(R.id.plan_spring);
        mAdapter=new PlanListAdapter(this,listData);
        listView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {
        initViewGONE();
        requstData();
    }

    /**
     * 加载数据
     */
    public void requstData(){
        Map<String,String> params=new HashMap<>();
        params.put("PlanStatus",""+status);
        params.put("Page",""+pageIndex);
        params.put("Rows","100");
        api.getBankList(this, jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                if (pageIndex==1){
                    listData.clear();
                }
                listData.addAll(JSON.parseArray(anyBean.getRankList(),RankListBean.class));
                mAdapter.refresh(listData);
                pageIndex++;
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    protected void setListener() {
        listView.setOnItemClickListener(this);
    }

    public void initViewGONE(){
        plan_success_view.setVisibility(View.GONE);
        plan_waiting_view.setVisibility(View.GONE);
        plan_performed_view.setVisibility(View.GONE);
        plan_error_view.setVisibility(View.GONE);
        plan_all_view.setVisibility(View.GONE);
        pageIndex=1;
    }

    @OnClick({R.id.iv_back,R.id.plan_all,R.id.plan_success_layout,R.id.plan_waiting_layout,R.id.plan_performed_layout,R.id.plan_error_layout,R.id.add_more})
    public void onClick(View view) {
        initViewGONE();
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.add_more:////新加
                startActivityIfNeeded(new Intent(this,BankPaymentReservationActivity.class),18);
                break;
            case R.id.plan_all:
                plan_all_view.setVisibility(View.VISIBLE);
                status=0;
                requstData();
                break;
            case R.id.plan_success_layout:
                plan_success_view.setVisibility(View.VISIBLE);
                status=4;
                requstData();
                break;
            case R.id.plan_waiting_layout:
                plan_waiting_view.setVisibility(View.VISIBLE);
                status=1;
                requstData();
                break;
            case R.id.plan_performed_layout:
                plan_performed_view.setVisibility(View.VISIBLE);
                status=3;
                requstData();
                break;
            case R.id.plan_error_layout:
                plan_error_view.setVisibility(View.VISIBLE);
                status=2;
                requstData();
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            switch (requestCode){
                case 18:
                    pageIndex=1;
                    requstData();
                    break;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent=new Intent(this,RePayPlanInfoActivity.class);
        intent.putExtra("id",listData.get(i).getPlanId());
        intent.putExtra("type",listData.get(i).getPlanStatus());
        startActivityIfNeeded(intent,18);
    }
}
